
import numpy as N

from gauss import gauss, BiasedGMM as GMMB
from qtAnimationWidget import *

class GMMCanvas(MatplotAnimationCanvas):
	
	def __init__(self, *args, **kwargs):
		MatplotAnimationCanvas.__init__(self, *args, **kwargs)
		self.initGMM()

	def initGMM(self):
		# TODO: from file
		self.mu = N.array([[91.1855], [69.7003], [54.9596]])
		self.va = N.array([[262.1538], [50.3270], [9.5023]])
		self.prior = N.array([[0.3533], [0.3823], [0.2644]])
		self.smaller_gaussian = 2

		self.gmm = GMMB(n_dim=1, n_states=3,  
				means=self.mu, 
				covars=self.va, 
				weights=self.prior)
		self.plotGMM()
	
	def plotGMM(self):
		self.axes.hold(True)
		ymix = N.array([0 for i in range(150)])
		x = N.array([j for j in range(150)])
		for i in range(3):
			y = gauss(x, self.va[i], self.mu[i])
			ymix = ymix+y
			self.axes.plot(x, y, 'r')
		
		self.axes.plot(x, ymix, 'b')

	def getSample(self, p):
		"""
		get one sample with p probability to achive a biased sample.
		biased sample means a sample achived by the third gaussian (the smaller one by configuration)
		return the sample as a float number and modality ('biased' or 'normal')
		"""
		#print 'GMMCanvas > getSample', p
		sample, mode = self.gmm.biasedSample(p, self.smaller_gaussian)
		return sample, mode


if __name__ == "__main__":
	import main.py
